<template>
  <div class="login-container">
    <div class="left">博之雅学生信息管理系统</div>
    <div class="login-form">
      <h2 class="login-title">欢迎登录</h2>
      <div class="box">
        <el-form :model="form" :rules="rules" ref="loginForm" label-width="0">
          <el-form-item>
            <el-input
              prefix-icon="el-icon-user"
              placeholder="账号"
              v-model="form.username"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-input
              prefix-icon="el-icon-lock"
              placeholder="密码"
              type="password"
              v-model="form.password"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-row>
              <el-col :span="10">
                <el-input
                  prefix-icon="el-icon-lock"
                  placeholder="验证码"
                  type="text"
                  v-model="form.captcha"
                ></el-input>
              </el-col>
              <el-col :offset="2" :span="6">
                <img
                  :src="captchaPic"
                  alt="点击重新获取验证码"
                  title="看不清，点击换一张"
                  style="width: 12rem; height: 35px"
                  @click="getCaptcha"
                />
              </el-col>
            </el-row>
          </el-form-item>

          <el-form-item class="remember-me">
            <el-checkbox v-model="form.remember" style="color: cornsilk"
              >记住我</el-checkbox
            >
            <a href="#" class="forgot-password">忘记密码？</a>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              class="login-button"
              @click="submitForm('loginForm')"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, computed } from "vue";
import {
  ElForm,
  ElFormItem,
  ElInput,
  ElButton,
  ElCheckbox,
  ElNotification,
} from "element-plus";
import {
  getAutoLogin,
  getCaptchaApi,
  postLogin,
} from "../services/api/login.js";
import router from "../router.js";
import { useStore } from "vuex";
import { onMounted } from "vue";
import { onBeforeMount } from "vue";

export default {
  components: {
    ElForm,
    ElFormItem,
    ElInput,
    ElButton,
    ElCheckbox,
  },
  setup() {
    const form = ref({
      username: "",
      password: "",
      captcha: "",
      id: "",
      remember: false,
    });
    const store = useStore();
    const captchaPic = ref("");

    const rules = ref({
      username: [{ required: true, message: "请输入账号", trigger: "blur" }],
      password: [{ required: true, message: "请输入密码", trigger: "blur" }],
    });

    const getCaptcha = async () => {
      let val = computed(() => store.state.isAuthenticated);
      await autoLogin();
      if (!val.value) {
        getCaptchaApi().then((res) => {
          form.value.id = res.data.data.id;
          captchaPic.value = res.data.data.captcha;
        });
      }
    };

    const submitForm = () => {
      postLogin(JSON.stringify(form.value)).then((res) => {
       
        if (res.data.code === 200) {
          let arrays = res.data.data;
          localStorage.setItem("menu", JSON.stringify(arrays));
          store.commit("login");
          router.push("/home");
          ElNotification({
            title: "login",
            message: "login success",
            type: "success",
          });
        }else if(res.data.msg==='验证码错误'){
          alert(res.data.msg+'，请检查验证码是否正确')
          form.value.captcha=''
          getCaptchaApi().then((res) => {
          form.value.id = res.data.data.id;
          captchaPic.value = res.data.data.captcha;
        });
        }else if(res.data.msg==='用户不存在或密码错误'){
          if(res.data.data==="没有此用户"){
            form.value.captcha=''
          getCaptchaApi().then((res) => {
           
          form.value.id = res.data.data.id;
          captchaPic.value = res.data.data.captcha;
        });
            alert(res.data.data+'，请检查账号是否正确')
           
          }else if(res.data.data==="密码错误"){
            alert(res.data.data+'，请检查密码是否正确')
            form.value.captcha=''
          getCaptchaApi().then((res) => {
          form.value.id = res.data.data.id;
          captchaPic.value = res.data.data.captcha;
        });
          }
        }
      });
    };

    const autoLogin = async () => {
      const res = await getAutoLogin();
      if (res.data.code === 200) {
        // let arrays = res.data.data;
        // localStorage.setItem("menu", JSON.stringify(arrays));
        // store.commit("login");
        router.push("/home");
        ElNotification({
          title: "login",
          message: "auto-login success",
          type: "success",
        });
      }
    };

    getCaptcha();

    return {
      form,
      rules,
      submitForm,
      captchaPic,
      getCaptcha,
    };
  },
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  height: 100vh;
  background: url("../assets/img/bg.jpg") no-repeat;
  background-size: cover;
}

.login-form {
  width: 20vw;
  height: 350px;
  margin-right: 20px;
  padding: 20px 100px;
  background-color: rgba(184, 177, 177, 0.5);
  border-radius: 8px;
  box-shadow: 0 0.3px 0.7px rgba(0, 0, 0, 0.126),
    0 0.9px 1.7px rgba(0, 0, 0, 0.179), 0 1.8px 3.5px rgba(0, 0, 0, 0.224),
    0 3.7px 7.3px rgba(0, 0, 0, 0.277), 0 10px 20px rgba(0, 0, 0, 0.4);
}

.login-title {
  text-align: center;
  font-size: 24px;
  margin-bottom: 20px;
  color: coral;
}

.remember-me {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.forgot-password {
  color: #999;
}

.login-button {
  width: 100%;
}
.left{
  font-family: Arial, Helvetica, sans-serif;
  margin-right: 5%;
  margin-bottom: 35%;
font-size: 60px;
font-weight: bold; /* 粗体 */
  color: coral; /* 标题字体颜色 */
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5); /* 添加阴影效果 */
  transform: skewX(-10deg); /* 水平拉伸效果 */
}
</style>
